package com.oa.controller;

import com.oa.pojo.Duty;
import com.oa.pojo.Result;
import com.oa.pojo.Role;
import com.oa.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色：在线接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @CrossOrigin
    @ApiOperation(value="查询用户角色(id,roleName)",httpMethod = "GET",notes = "返回包含用户类的result类型json格式")
    @GetMapping("/selectAll")
    public Result selectAll(Integer id,String roleName){
        System.out.println("执行查询用户角色接口");
        Role role = new Role();
        role.setId(id);
        role.setRoleName(roleName);
        List<Role> roleList = roleService.selectRole(role);

        return Result.success(roleList);
    }
    @CrossOrigin
    @ApiOperation(value="根据角色id删除(id)",httpMethod = "GET",notes = "返回包含用户类的result类型json格式")
    @GetMapping("/deleteRole")
    public Result deleteRole(Integer id){
        System.out.println("执行角色删除接口");

        int flag= roleService.deleteRole(id);
        if (flag==1){
            return Result.success();
        }else {
            return Result.error("没有此id的角色");
        }
    }

    @CrossOrigin
    @ApiOperation(value="角色信息更改",httpMethod = "POST",notes = "返回的result类型json格式")
    @PostMapping("/updateRole")
    public Result updateRole(@RequestBody Role role){
        System.out.println("执行角色信息更改接口");

        int flag=roleService.updateRole(role);
        if (flag==1) {
            return Result.success();
        }else {
            return Result.error("添加新角色失败，有重复属性");
        }
    }

    @CrossOrigin
    @ApiOperation(value="添加角色",httpMethod = "POST",notes = "返回的result类型json格式")
    @PostMapping("/insertRole")
    public Result insertRole(@RequestBody Role role){
        System.out.println("执行角色添加功能");

        int flag= roleService.insertRole(role);

        if (flag==1) {
            return Result.success();
        }else {
            return Result.error("添加心部门失败，有重复属性");
        }
    }
}
